<script src="/js/jquery.js"></script>
<style>
.region_list{margin-bottom: 15px;}

   
   
#province{width:100px;font-family: inherit; font-size: inherit; line-height: inherit;padding: 3px 12px;}
#city{width:100px;font-family: inherit; font-size: inherit; line-height: inherit;padding: 2px 12px;}
#district{width:100px;font-family: inherit; font-size: inherit; line-height: inherit;padding: 2px 12px;}
.region_label{  display: inline-block; font-weight: bold; margin-bottom: 5px;max-width: 100%;}
</style>
<?php 
use common\models\BaseRegion;
?>

<div class="region_list">
<?php if (!$model->isNewRecord) {?>
<?=$form->field($model, 'province')->dropDownList(BaseRegion::getRegionById(1),['prompt'=>'请选择','style'=>'width:200px;','id'=>'province']);?>
<?=$form->field($model, 'city')->dropDownList(BaseRegion::getRegionById($model->province),['prompt'=>'请选择','style'=>'width:200px;','id'=>'city']);?>
<?=$form->field($model, 'district')->dropDownList(BaseRegion::getRegionById($model->city),['prompt'=>'请选择','style'=>'width:200px;','id'=>'district']);?>
<?php  }else{ ?>
<?=$form->field($model, 'province')->dropDownList(BaseRegion::getRegionById(1),['prompt'=>'请选择','style'=>'width:200px;','id'=>'province']);?>
<?=$form->field($model, 'city')->dropDownList([],['prompt'=>'请选择','style'=>'width:200px;','id'=>'city']);?>
<?=$form->field($model, 'district')->dropDownList([],['prompt'=>'请选择','style'=>'width:200px;','id'=>'district']);?>
<?php }?>
</div>					
<script type="text/javascript">
$(function() {
	/**
	 * @Description: 根据省份名称获取其下属城市
	 * @author: yulong.wang
	 * @date: 2014-11-20 下午7:53:32
	 */
	$("#province").change(function(){
		var self = $(this);
		$.ajax({
			url : '<?= f_url(['common/region-by-id']) ?>',
			type : 'get',
			dataType : 'JSON',
			data : {pid : self.val()},
			success : function (data) {
					var option="<option value=0>请选择</option>";
					$.each(data,function(n,value) {
							option+="<option value="+n+" id=city"+">"+value+"</option>"
					   });
					$("#city").html(option);	
					var op = "<option value=0>请选择</option>";
					$("#district").html(op);
			} 
		});
	})
	
	/**
	 * @Description: 根据省份名称获取其下属城市
	 * @author: yulong.wang
	 * @date: 2014-11-20 下午7:53:32
	 */
	$("#city").change(function(){
		var self = $(this);
		$.ajax({
			url : '<?= f_url(['common/region-by-id']) ?>',
			type : 'get',
			dataType : 'JSON',
			data : {pid : self.val()},
			success : function (data) {
					var option="<option value=0>请选择</option>";
					$.each(data,function(n,value) {
							option+="<option value="+n+" id=district"+">"+value+"</option>"
					   });
					$("#district").html(option);		
			} 
		});
	})

	$(".btn-primary").click(function (){
       	 	var provinceValue = $("#province").val();
       		var cityValue = $("#city").val();
       		var districtValue = $("#district").val();
    		if (provinceValue==0  || cityValue==0 || districtValue==0) {
     			    alert("请完善区域");
            		return false;
    		   }
    })
})
</script>
